package dp.leetcode_121;

public class GreedSolution {
    public int maxProfit(int[] prices) {
        if (prices.length == 0) {
            return 0;
        }
        // minPrice 记录历史最低点的股票价格
        int minPrice = prices[0];
        int maxProfit = 0;
        for (int i = 0; i < prices.length; i++) {
            // 在历史最低点这一天买入股票
            minPrice = Math.min(minPrice, prices[i]);
            // 在股票价格最高这一天卖出
            maxProfit = Math.max(maxProfit, prices[i] - minPrice);
        }
        return maxProfit;
    }
}
